﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using Common;
using DemoManageSys.Areas.Filters;
using IBLL;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Models;
using Models.DTO;

namespace DemoManageSys.Areas.Admin.Controllers
{
    [Area("Admin")]
    [MyFilter]
    public class PurchaseController : Controller
    {
        private IPurchaseBLL _purchaseBLL;

        public PurchaseController(
            IPurchaseBLL purchaseBLL
            )
        {
            _purchaseBLL = purchaseBLL;
        }

        #region 显示视图
        public IActionResult ListView()
        {
            return View();
        }

        public IActionResult CreateView()
        {
            return View();
        }

        public IActionResult EditView()
        {
            return View();
        }
        #endregion

        #region 获取进货信息表的方法 (GetPurchasees)
        /// <summary>
        /// 获取进货信息表的方法
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="purchaseName"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetPurchase(int page, int limit, string purchaseName)
        {
            int count;
            List<GetPurchaseDTO> GetPurchaseDTOs = _purchaseBLL.GetPurchase(page, limit, purchaseName, out count);
            ReturnResule result = new ReturnResule()
            {
                Code = 0,
                Msg = "获取成功",
                Data = GetPurchaseDTOs,
                Count = count
            };

            return Json(result);
        }
        #endregion

        #region 添加的方法 (CreatePurchase)
        /// <summary>
        /// 添加的方法
        /// </summary>
        /// <param name="menu"></param>
        /// <returns></returns>
        public IActionResult CreatePurchase([FromForm] Purchase cons)
        {
            string msg;
            ReturnResule resule = new ReturnResule();

            resule.IsSuccess = _purchaseBLL.CreatePurchase(cons, out msg);
            if (resule.IsSuccess)
            {
                resule.Code = 200;
            };
            resule.Msg = msg;

            return Json(resule);
        }
        #endregion

        #region 删除的方法 (DeletePurchase)
        /// <summary>
        ///删除的方法
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult DeletePurchase(string id)
        {
            ReturnResule result = new ReturnResule();
            result.IsSuccess = _purchaseBLL.DeletePurchase(id);
            if (result.IsSuccess)
            {
                result.Code = 200;
            }
            result.Msg = result.IsSuccess ? "删除成功" : "删除失败";

            return Json(result);
        }
        #endregion

        #region 修改的方法 (UpdatePurchase)
        /// <summary>
        /// 修改的方法
        /// </summary>
        /// <param name="menu"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult UpdatePurchase([FromForm] Purchase cons)
        {
            //实例化对象
            ReturnResule resule = new ReturnResule();

            string msg;//定义返回消息
            resule.IsSuccess = _purchaseBLL.UpdatePurchase(cons, out msg);//调用方法传值

            resule.Msg = msg;
            if (resule.IsSuccess)
            {
                resule.Code = 200;
            }

            return Json(resule);
        }
        #endregion

        #region 批量软删除菜单的方法 (DeleteListPurchase)
        /// <summary>
        /// 批量软删除菜单的方法
        /// </summary>
        /// <param name="user">用户实体</param>
        /// <param name="msg">返回消息</param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult DeleteListPurchase(List<string> ids)
        {
            //实例化对象
            ReturnResule resule = new ReturnResule();
            if (ids.Count == 0)
            {
                resule.Msg = "你没有返回任何进货";
                return Json(resule);
            }
            resule.IsSuccess = _purchaseBLL.DeleteListPurchase(ids);//调用方法传值
            if (resule.IsSuccess)
            {
                resule.Code = 200;
            }
            resule.Msg = resule.IsSuccess?"删除进货成功": "删除进货失败";

            return Json(resule);
        }
        #endregion

        #region 根据ID获取下拉框 (GetPurchaseById)
        /// <summary>
        /// 根据ID获取下拉框
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetPurchaseById(string id)
        {
            ReturnResule resule = new ReturnResule();

            if (string.IsNullOrWhiteSpace(id))
            {
                resule.Msg = "ID为空";
                return Json(resule);
            }

            Purchase purc = _purchaseBLL.GetPurchaseById(id);
            if (purc == null)
            {
                resule.Msg = "未获取到信息";
            }
            else
            {
                var selectOption = _purchaseBLL.GetSelectOption();
                resule.Msg = "成功";
                resule.Code = 200;
                resule.Data = new
                {
                    purc = purc,//菜单信息
                    SelectOption = selectOption//下拉列表数据
                };
            }
            return Json(resule);
        }
        #endregion

        #region 获取下拉框的方法 (GetSelectOption)
        /// <summary>
        /// 获取下拉框的方法
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetSelectOption()
        {
            ReturnResule resule = new ReturnResule();
            var data = _purchaseBLL.GetSelectOption();

            if (data != null)
            {
                resule.Msg = "返回成功";
                resule.Data = data;
                resule.Code = 200;
            }
            return Json(resule);
        }
        #endregion

        #region Excel进货入库接口 (UpLoad)
        public IActionResult UpLoad(IFormFile file)
        {
            ReturnResule resule = new ReturnResule();
            string msg;

            var userId = HttpContext.Request.Cookies["UserId"];
            if (userId == null)
            {
                resule.Msg = "入库人ID不能为空";
                return Json(resule);
            }
            if(file == null)
            {
                resule.Msg = "你没有选择上传的文件";
                return Json(resule);
            }

            string fileName = file.FileName;
            string extension = Path.GetExtension(fileName);//获取文件后缀,带.(点)如.cs
            if(extension == ".xlsx"||extension==".xls")
            {
                Stream stream = file.OpenReadStream();
                resule.IsSuccess = _purchaseBLL.UpLoad(stream, extension, userId, out msg);
                if (resule.IsSuccess)
                {
                    resule.Code = 200;
                }
                resule.Msg = msg;
                return Json(resule);
            }
            else
            {
                resule.Msg = "你没有选择正确的文件";
                return Json(resule);
            }
        }
        #endregion

        #region 出入库记录的接口 (GetDownLoad)
        /// <summary>
        /// 出入库记录的接口
        /// </summary>
        /// <returns></returns>
        public IActionResult GetDownLoad()
        {
            string downLoadName;

            Stream stream = _purchaseBLL.GetDownLoad(out downLoadName);

            //返回文件流
            return File(stream, "application/octet-stream", downLoadName);
        }
        #endregion
    }
}